import React, { useState } from 'react'
import { Button, Form, Input, message } from 'antd'
import { formItemLayout } from '../const'
import { setTicket } from '../../request/setting'

function Cmcc(props) {
  const [isLoading, setIsLoading] = useState(false)
  const { getFieldDecorator, validateFields } = props.form

  const onSubmit = () => {
    validateFields(async(err, values) => {
      if (err) {
        return false
      }
      setIsLoading(true)
      const { code, message: errMsg } = await setTicket(values)
      setIsLoading(false)
      if (code) {
        return message.error(errMsg)
      }
      message.success('设置成功')
    })
  }

  return (
    <div className="cmcc">
      <Form {...formItemLayout}>
        <Form.Item label="ticket">
          {getFieldDecorator('ticket', {
            rules: [
              { required: true, message: 'ticket不能为空' }
            ]
          })(
            <Input placeholder="请输入ticket"/>
          )}
        </Form.Item>
        <Form.Item label=" " colon={false}>
          <Button type="primary" loading={isLoading} onClick={onSubmit}>保存</Button>
        </Form.Item>
      </Form>
    </div>
  )
}

export default Form.create()(Cmcc)
